package com.webtair.dump4j.producer;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.webtair.dump4j.core.Item;
import com.webtair.dump4j.objectdump.ObjectDump;

public class ProducerStringArray implements Producer{
//	ObjectDumpStringList obj;
	ObjectDump obj;
	int shiftSize = 4;
	public ObjectDump prepareDump(Item root, ObjectDump obj) {
		//obj = new ObjectDumpStringList();
		this.obj = obj; 
		write(root);
		return obj;
	}

	private void write(Item root) {
		String s = generateString(root);
		obj.addBlock(s);
		List l = root.getChildren();
		for (Iterator iter = l.iterator(); iter.hasNext();) {
			Item element = (Item) iter.next();
			write(element);
		}
	}

	private String generateString(Item item) {
		StringBuffer buffer = new StringBuffer();
		String shift="";
		String semicol = ": ";
		int level = item.getLevel();
		if(level>0){
			char[] arr = new char[level*shiftSize];
			Arrays.fill(arr,' ');
			shift = new String(arr);
		}
		buffer.append(shift);
		buffer.append(item.getName());
		buffer.append(semicol);

		if(item.getValue()!=null){
			buffer.append("value="+item.getValue()+" ");
		} else {
			buffer.append("id="+item.getId()+ " ");
		}

		buffer.append("("+item.getClassName()+")");
		
		return buffer.toString();
	}



}
